﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

using HotelManagement.DAO;
using HotelManagement.Connector;

namespace RoomBookingManagement
{
    public partial class RoomBookingManagement : Form
    {
        RoomBookingManagementDAO m_dao;

        public RoomBookingManagement()
        {
            InitializeComponent();

            m_dao = new RoomBookingManagementDAO();
        }

        private void search(object sender, EventArgs e)
        {
            DataTable table = m_dao.search(customerName.Text);

            bookedData.DataBindings.Clear();
            bookedData.DataSource = table;

            Invalidate();
        }

        private void closeForm(object sender, EventArgs e)
        {
            this.Close();
        }

        private void selectRow(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView data = sender as DataGridView;
            if (data == null)
                return;

            if(data.CurrentRow.Selected)
            {
                customerName.Text = data.CurrentRow.Cells[7].Value.ToString();
                customerCode.Text = data.CurrentRow.Cells[5].Value.ToString();
                nPeople.Text = data.CurrentRow.Cells[2].Value.ToString();
                inDate.Value = ((DateTime)data.CurrentRow.Cells[3].Value);
                outDate.Value = ((DateTime)data.CurrentRow.Cells[4].Value);
                roomCode.Text = data.CurrentRow.Cells[0].Value.ToString();
            }
        }

        private void update(object sender, EventArgs e)
        {
            m_dao.update(roomCode.Text, int.Parse(nPeople.Text), inDate.Value, outDate.Value, int.Parse(customerCode.Text));
            bookedData.Update();
            search(null, null);
        }

        private void removeBooking(object sender, EventArgs e)
        {
            m_dao.erase(roomCode.Text);
            bookedData.Update();
            search(null, null);
        }
    }
}
